package com.railway.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.railway.service.UserService;
import com.railway.service.impl.UserServiceImpl;

/**
 * 手机核验
 *
 */

@WebServlet("/MobilePhoneVerification.do")
public class MobilePhoneVerificationServlet extends HttpServlet{

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doPost(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		req.setCharacterEncoding("utf-8");
		resp.setCharacterEncoding("UTF-8");
		resp.setContentType("application/json");// 后端告知前端 返回的数据 是 json格式(不是html /css)
		String newphone = req.getParameter("newphone");//新手机号
		String password = req.getParameter("password");//密码
		String username = req.getParameter("username");//用户名
		if(newphone == null) {
			newphone="";
		}else {
			newphone = req.getParameter("newphone");
		}
		
		if(password == null) {
			password="";
		}else {
			password = req.getParameter("password");
		}
		UserService us = new UserServiceImpl();
		if(password == "" || newphone == ""){
			req.setAttribute("failUpdate", "密码或手机号不能为空");
			resp.sendRedirect("refund.jsp");
			return;
		}else {
			int phoneUpdate = us.updatePhone(newphone, username, password);
			ObjectMapper om = new ObjectMapper(); 
			resp.getWriter().print(om.writeValueAsString(phoneUpdate));
		}
		
	}
	
}
